package com.ziyun.pop.web.modules.storeMaterials.controller;

import java.util.List;
import java.util.Map;

import com.ziyun.pop.common.e.WhetherEnum;
import com.ziyun.pop.common.e.material.DirectionEnum;
import com.ziyun.pop.common.e.material.FinancePaymentStatusEnum;
import com.ziyun.pop.common.e.material.StockMaterialIoTypeEnum;
import com.ziyun.pop.common.e.material.StoreMaterialInStatusEnum;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.purchase.entity.dto.StorePurchaseApplyDto;
import com.ziyun.pop.modules.storeMaterials.entity.StoreMaterialInDetailEntity;
import com.ziyun.pop.modules.storeMaterials.entity.StorematerialinOutEntity;
import com.ziyun.pop.modules.storeMaterials.entity.dto.StoreMaterialInDto;
import com.ziyun.pop.modules.storeMaterials.service.ErpStockService;
import com.ziyun.pop.modules.storeMaterials.service.StoreMaterialInDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.pop.modules.storeMaterials.entity.StoreMaterialInEntity;
import com.ziyun.pop.modules.storeMaterials.service.StoreMaterialInService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 门店耗材入库表
 *
 * @author guizhao
 * @email 296588519@qq.com
 * @date 2018-10-04 10:03:15
 */
@RestController
@RequestMapping("storeMaterials/storematerialin")
public class StoreMaterialInController {
    @Autowired
    private StoreMaterialInService storeMaterialInService;

    @Autowired
    private ErpStockService erpStockService;

    @Autowired
    private StoreMaterialInDetailService storeMaterialInDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeMaterials:storematerialin:list")
    public R list(@RequestParam Map<String, Object> params){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Integer stockId = erpStockService.getStockId(sellerId);
        params.put("stockId",stockId);
        PageUtils page = storeMaterialInService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查询入库信息
     */
    @RequestMapping("/queryInDetial")
    public R queryInDetial(@RequestBody Map<String,Object> params){
        return storeMaterialInService.queryInDetial(params);
    }

    /**
     * 保存
     */
    @RequestMapping("/saveIn")
//    @RequiresPermissions("storeMaterials:storematerialin:save")
    public R saveIn(@RequestBody StoreMaterialInDto storeMaterialInDto){
        return storeMaterialInService.saveIn(storeMaterialInDto);
    }

    /**
     * 获取枚举类
     */
    @RequestMapping("/getEnum")
    public R getEnum(){
        List<Map<String, Object>> whetherEnum = JsonUtils.enumToList(WhetherEnum.class);
        List<Map<String, Object>> stockMaterialIoTypeEnum = JsonUtils.enumToList(StockMaterialIoTypeEnum.class);
        List<Map<String, Object>> directionEnum = JsonUtils.enumToList(DirectionEnum.class);
        List<Map<String, Object>> storeMaterialInStatusEnum = JsonUtils.enumToList(StoreMaterialInStatusEnum.class);
        List<Map<String, Object>> financePaymentStatusEnum = JsonUtils.enumToList(FinancePaymentStatusEnum.class);

        return R.ok()
                .put("whetherEnum", whetherEnum)
                .put("stockMaterialIoTypeEnum", stockMaterialIoTypeEnum)
                .put("directionEnum", directionEnum)
                .put("storeMaterialInStatusEnum", storeMaterialInStatusEnum)
                .put("financePaymentStatusEnum", financePaymentStatusEnum);
    }


    /**
     * 入库审核
     */
    @RequestMapping("/audit/{id}")
    public R audit(@PathVariable("id") Integer id){
        return storeMaterialInService.audit(id);
    }

    /**
     * 入库驳回
     */
    @RequestMapping("/reject/{id}")
    public R reject(@PathVariable("id") Integer id){
        return storeMaterialInService.reject(id);
    }

    /**
     * 盘存提交
     */
    @RequestMapping("/save")
    public R save(@RequestBody StorePurchaseApplyDto storePurchaseApplyDto){
        return storeMaterialInService.inventory(storePurchaseApplyDto,UserUtils.getCurentUser());
    }

    /**
     * 获取入库主表信息
     */
    @RequestMapping("/info/{inId}")
    public R getInfo(@PathVariable("inId") Integer inId){
        Integer stockByInid = storeMaterialInService.getStockByInId(inId);
        String stockName = erpStockService.getStockName(stockByInid);
        StoreMaterialInEntity infoData = storeMaterialInService.getInfoData(inId);
        return R.ok().put("infoData",infoData).put("stockName",stockName);
    }

    /**
     * 获取入库明细信息
     */
    @RequestMapping("storematerialInDetailList/{inId}")
    public R storematerialinInDetailList(@PathVariable("inId") Integer inId){
        String inNo = storeMaterialInService.getInNoByinId(inId);
        List<StoreMaterialInDetailEntity> liststorematerialIndetail = storeMaterialInDetailService.getListstorematerialIndetail(inId, inNo);
        return R.ok().put("liststorematerialIndetail",liststorematerialIndetail);
    }
    /**
     * 保存单据号
     */
    @RequestMapping("/editOtherAccessory/{inId}/{ids}")
    public R editOtherAccessory(@PathVariable("inId") Integer inId,@PathVariable("ids") String ids){
        return storeMaterialInService.editOtherAccessory(inId,ids);
    }



}
